/**
 * @Title: GoodInfoActivity.java
 * @Copyright: KJ Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * @Description: 商品详情Activity
 * @author: zhgk
 * @data: 815-08-10 16:37:00
 * @version: V1.0
 */
package com.kbt.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.android.volley.Request;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.kbt.adapter.GoodInfoAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.listener.clickOpenWindowListener;
import com.kbt.model.data.*;
import com.kbt.net.RequestManager;
import com.kbt.testimageview.RollViewPagerShopInfo;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.*;
import com.kbt.util.utils.*;
import com.readystatesoftware.viewbadger.BadgeView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * $description: 商品详情Activity
 *
 * @author MeiYi
 * @date: ${date} ${time}
 * @version: V1.0
 */
public class GoodInfoActivity extends Activity implements RequestManager.ResponseInterface{
    public BadgeView carBadge;
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private Map<String, String> mappost =new HashMap<String, String>();
    private ShopInfos shopInfos;
    private List<ShopInfo> goodInfos =new ArrayList<ShopInfo>();
    private ShopInfo goodInfo;
    private String shopId;
    private BaseBean baseBean;
    private CountBean countBean;
    private LinearLayout shopInfoLin;
    private String phone;
    private SharePreferenceUtils share;
    private List<String> goodInfoImageList = new ArrayList<String>();
    private NoScrollListView goodInfpListView;
    private  GoodInfoAdapter goodInfoAdapter;
    private String []imageURL;
    private Boolean isRefreshing = false;
    private int xS=0;//显示图片数量
    private PullToRefreshScrollView pullScrollView;
    private TextView info_goodFrom;
    private LoadingDialog mProgress;
    private LinearLayout time;
    private LinearLayout lin_two_btn;
    private TextView JianCeRiQi;
    private TextView info;
    private TextView checkBtn_jianCe;
    private LinearLayout linTop;

    private ArrayList<View> dots = null;
    private LinearLayout mViewPagerLay;// 只需要一个layout即可,ViewPager是动态加载的
    private ArrayList<String> uriList = null;
    private ImageView dot_0;
    private ImageView dot_1;
    private ImageView dot_2;
    private ImageView dot_3;
    private ImageView dot_4;
    private ImageView dot_5;
    private ImageView dot_6;
    private ImageView dot_7;
    private ImageView dot_8;
    private ImageView dot_9;
    private RollViewPagerShopInfo mViewPager;

    private Boolean isCheck=true;//下拉还是上拉（true）

    private TextView info_time;//倒计时
    private long hours;
    private long minutes;
    private long seconds;
    private long diff;
    private long days;
    private long haomiao ;
    private InfoHandler infoHandler;
    private List<Goods> goodss = new ArrayList<Goods>();

    private DictBean dictBean;
    private DictListBean dictListBean;
    private List<DictBean> dicList = new ArrayList<DictBean>();

    private TextView production;




    /**
     * @Title:
     * @Description: 重写父类Activity
     * @param: Bundle savedInstanceState
     * @return: void
     * @throws: null
     * @author: zhgk
     * @date: 815-08-10 16:37:00
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        //重写标题栏
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.good_info_layout);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);
        //设置标题内容
        TextView textView = (TextView) findViewById(R.id.center_title);
        textView.setText(StaticData.GOODS_INFO_TITLE);
        Intent intent = getIntent();
        shopId =intent.getStringExtra("shopId");
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        share = new SharePreferenceUtils(GoodInfoActivity.this);
        phone = share.getString("user_mobile","");
        goodInfpListView = (NoScrollListView) findViewById(R.id.goodInfpList);
        goodInfpListView.setDividerHeight(0);
        pullScrollView = (PullToRefreshScrollView) findViewById(R.id.pullScrollView);
        pullScrollView.setMode(PullToRefreshBase.Mode.BOTH);//设置上拉，下拉
        goodInfoAdapter = new GoodInfoAdapter(this,goodInfoImageList);
        goodInfpListView.setAdapter(goodInfoAdapter);
        time = (LinearLayout) findViewById(R.id.time);
        lin_two_btn = (LinearLayout) findViewById(R.id.lin_two_btn);
        JianCeRiQi = (TextView) findViewById(R.id.JianCeRiQi);
        checkBtn_jianCe = (TextView) findViewById(R.id.checkBtn_jianCe);
        BtnSetting.setTextViewBackground(checkBtn_jianCe, 1, Color.RED, Color.WHITE, 5);
        linTop = (LinearLayout) findViewById(R.id.linTop);
        info_time = (TextView) findViewById(R.id.info_time);
        production = (TextView) findViewById(R.id.production);

        if(getIntent().getStringExtra("tag")!=null) {
            if (getIntent().getStringExtra("tag").equals("kaiQiang")) {
                lin_two_btn.setVisibility(View.GONE);
            }
        }
        httpShopInfo();
        initListener();
    }
    //请求商品详情
    private void httpShopInfo(){
        showProgress();
        if(Utils.isNetworkAvailable(GoodInfoActivity.this)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.shopInfoURL + shopId, Constants.RequestTag.shopInfoTag, map, ShopInfos.class);
        }else{
            Toast.makeText(GoodInfoActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    //监听
    private void initListener(){
        checkBtn_jianCe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodInfoActivity.this, CheckActivity.class);
                intent.putExtra("zhiJianImage", goodInfo.getGoods_report_img_url());
                startActivity(intent);
            }
        });
    }

    /***
     * 重写启动方法
     */
    @Override
    protected void onStart() {
        super.onStart();
        BaseApplication.getInstance().addActivity(GoodInfoActivity.this);
        //返回按钮事件
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.getInstance().removeActivity(GoodInfoActivity.this);
                finish();
            }
        });
    }

    /**
     * 初始化商品详情信息
     */
    private void initGoodsInfo() {
        //售完
        ImageView imageView = (ImageView) findViewById(R.id.shopInfo_shouwan);
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth()/4;
        String lunboImage = goodInfo.getGoods_title_img_url();
        String[] lunboImageArray =  lunboImage.split(",");
        ViewGroup.LayoutParams image = imageView.getLayoutParams();
        image.width = screenWidth;
        String[] imageurl = lunboImageArray[0].replaceAll("\"","").split("&");
        int imageWidth2 = 0;
        int imageHeight2 = 0;
        if(imageurl.length == 3){
            imageWidth2 = Integer.parseInt(imageurl[1].split("=")[1]);
            imageHeight2 = Integer.parseInt(imageurl[2].split("=")[1]);
        }
        if(imageWidth2>0 && imageHeight2>0){
            image.height = image.width;
        }else{
            image.height = image.width;
        }
        imageView.setLayoutParams(image);
        imageView.setMaxWidth(screenWidth);
        imageView.setMaxHeight(screenWidth * 10);

        //库存
        TextView kucun = (TextView) findViewById(R.id.kucun);

        //库存为0
        if(goodInfo.getStore_counts().equals("0")){
            kucun.setText("已售完");
            lin_two_btn.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
        }else{
            kucun.setText("库存:" + goodInfo.getStore_counts());
        }
        if(isCheck) {
            if (infoHandler == null) {
                infoHandler = new InfoHandler();
            }

            if (getTimeCha(goodInfo.getStart_time())) {
                goodss.add(new Goods(getTime(getNewTime(), goodInfo.getStart_time() + ":000")));
                lin_two_btn.setVisibility(View.GONE);
//                imageView.setBackgroundResource(R.drawable.kaiqiang);
//                imageView.setVisibility(View.GONE);
            } else {
                goodss.add(new Goods(getTime(getNewTime(), goodInfo.getEnd_time() + ":000")));
            }
            infoHandler.sendEmptyMessage(1);
        }


        //商品说明
        info_goodFrom = (TextView) findViewById(R.id.info_goodFrom);
        info  = (TextView) findViewById(R.id.info);
        String infoText = goodInfo.getGoods_desc();
        info.setText(Html.fromHtml(infoText.replace("\n","<br />")));
        //查询字典
        httpDic();

        if(goodInfo.getGoods_examination_time()!=null&&!"".equals(goodInfo.getGoods_examination_time())&&goodInfo.getGoods_make_time()!=null&&!"".equals(goodInfo.getGoods_make_time())) {
            JianCeRiQi.setText("本批次产品"+" 生产日期:" + goodInfo.getGoods_make_time()+"    检测报告日期:" + goodInfo.getGoods_examination_time());
            time.setVisibility(View.VISIBLE);
        }else if(goodInfo.getGoods_examination_time()!=null&&!"".equals(goodInfo.getGoods_examination_time())){
            JianCeRiQi.setText("本批次产品检测报告日期:" + goodInfo.getGoods_examination_time());
            time.setVisibility(View.VISIBLE);
        }else if(goodInfo.getGoods_make_time()!=null&&!"".equals(goodInfo.getGoods_make_time())){
            JianCeRiQi.setText("本批次产品生产日期:" + goodInfo.getGoods_make_time());
            time.setVisibility(View.VISIBLE);
        }else{
            time.setVisibility(View.GONE);
        }
        //销量
        TextView xiaoliang = (TextView) findViewById(R.id.xiaoliang);
        xiaoliang.setText("销量:"+goodInfo.getGoods_sale_count());
        //商品名称
        TextView goodTitleTextView = (TextView) findViewById(R.id.info_title);
        goodTitleTextView.setText(goodInfo.getGoods_title());
        //商品现价
        TextView goodNowPriceTextView = (TextView) findViewById(R.id.info_nowPrice);
        goodNowPriceTextView.setText("￥" + goodInfo.getGoods_new_price());

        //商品原价
        TextView goodOldPriceTextView = (TextView) findViewById(R.id.info_oldPrice);
        goodOldPriceTextView.setText("￥" + goodInfo.getGoods_price());
        goodOldPriceTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        //图片容器
        LinearLayout goodInfoImgContainer = (LinearLayout) findViewById(R.id.info_info_img_layout);

        //立即购买按钮
        Button buyNowBtn = (Button) findViewById(R.id.buyNow);
        clickOpenWindowListener buyNowBtnOnClickListener = new clickOpenWindowListener(this,
                goodInfoImgContainer, StaticData.PopWindow_buyNow, StaticData.PopWindowFromGoodInfo,goodInfo);
        buyNowBtn.setOnClickListener(buyNowBtnOnClickListener);
        //加入购物车
        Button addCarBtn = (Button) findViewById(R.id.addCar);

        clickOpenWindowListener addCarOnClickListener = new clickOpenWindowListener(this,
                goodInfoImgContainer, StaticData.PopWindow_addCar, StaticData.PopWindowFromGoodInfo,goodInfo);
        addCarBtn.setOnClickListener(addCarOnClickListener);

        TextView carCountView = (TextView) findViewById(R.id.carCountView);

        //购物车
//        Button goodInfoCarBtn = (Button) findViewById(R.id.good_info_car);
        shopInfoLin = (LinearLayout) findViewById(R.id.shopInfoLin);
        //创建背景形状
        GradientDrawable drawable = new GradientDrawable();
        //设置边框
        drawable.setShape(GradientDrawable.RECTANGLE);
        //设置边框长度和颜色
        drawable.setStroke(2, Color.GRAY);
        //设置背景色
        drawable.setColor(Color.WHITE);
        //设置圆角
        drawable.setCornerRadius(5);
        //设置按钮背景
        shopInfoLin.setBackground(drawable);
        carBadge = new BadgeView(this, carCountView);
        if(share.getString("user_mobile","")!=null&&!"".equals(share.getString("user_mobile",""))) {
            httpSelectShopType();
        }

        //购物车
        Map params = new HashMap();
        params.put("currentItem", StaticData.INDEX_CAR);

        //加入购物车点击事件
        shopInfoLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(BaseApplication.getInstance().isLogin()){
                    BaseApplication.getInstance().currentItem=StaticData.INDEX_CAR;
                    BaseApplication.getInstance().carStatus=StaticData.GOOD_CAR_COMPLETE;
                    CommonStatus.carStatus = StaticData.GOOD_CAR_COMPLETE;
                    BaseApplication.getInstance().finishActivitys();//关闭activity
                    Intent intent = new Intent(GoodInfoActivity.this,StoreActivity.class);
                    startActivity(intent);
                    BaseApplication.getInstance().removeActivity(GoodInfoActivity.this);
                    finish();
                }else{
                    Intent intent = new Intent(GoodInfoActivity.this,LoginActivity.class);
                    startActivity(intent);
                    BaseApplication.getInstance().removeActivity(GoodInfoActivity.this);
//                    finish();
                }
            }
        });
        pullScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (!isRefreshing) {
                    isRefreshing = true;
                    if (refreshView.isFooterShown()) {
                        linTop.setVisibility(View.GONE);
                        goodInfpListView.setVisibility(View.VISIBLE);
                        refresh(true);
                    } else if (refreshView.isHeaderShown()) {
                        linTop.setVisibility(View.VISIBLE);
                        goodInfpListView.setVisibility(View.GONE);
                        refresh(false);
                    }
                } else {
                    refreshView.onRefreshComplete();
                }
            }
        });

    }
    //字典
    public void httpDic(){
        showProgress();
        if(Utils.isNetworkAvailable(GoodInfoActivity.this)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.dicURL, Constants.RequestTag.dicTag, map, DictListBean.class);
        }else{
            stopProgress();
            Toast.makeText(GoodInfoActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    protected void refresh(Boolean isNextPage) {
        isCheck =isNextPage;
        if(isNextPage) {
            try {
                if (imageURL.length > 0) {
                        if(xS==imageURL.length){
                            Toast.makeText(GoodInfoActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
                        }
                    //一下加载8张
//                        if(imageURL.length-xS>=8){
//                            xS+=8;
//                        }else{
//                            xS=imageURL.length;
//                        }
                    //一下加载全部
                        xS =imageURL.length;
                        goodInfoImageList.clear();
                        for (int i = 0; i < xS; i++) {
                            if (!",".equals(imageURL[i])) {
                                goodInfoImageList.add(imageURL[i]);
                            }
                        }
                        if (goodInfoAdapter != null) {
                            goodInfoAdapter.setList(goodInfoImageList);
                            goodInfoAdapter.notifyDataSetChanged();
                        } else {
                            goodInfoAdapter = new GoodInfoAdapter(this, goodInfoImageList);
                            goodInfpListView.setAdapter(goodInfoAdapter);
                        }
                        pullScrollView.postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                pullScrollView.onRefreshComplete();
                            }
                        }, 1000);
                        isRefreshing = false;

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            imageURL =null;
            goodInfoImageList.clear();
            xS=8;
            httpShopInfo();
            isRefreshing=false;
            pullScrollView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    pullScrollView.onRefreshComplete();
                }
            }, 1000);
        }
    }

    //查询该商品的数量
    private void httpSelectShopType(){
        showProgress();
        if(Utils.isNetworkAvailable(GoodInfoActivity.this)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectShopCarZhongLeiURL + phone + "&methodType=queryGoodsSum", Constants.RequestTag.selectShopCarZhongLeiTag, map, CountBean.class);
        }else{
            stopProgress();
            Toast.makeText(GoodInfoActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        stopProgress();
        if (tag == Constants.RequestTag.shopInfoTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                shopInfos = (ShopInfos) parsedGSON;
                    goodInfo = shopInfos.getData();
                if(goodInfo!=null) {
                    String image = goodInfo.getGoods_info_img_url();
                    imageURL = image.split(",");
                    initGoodsInfo();
                    //下拉不走
                    if (isCheck) {
                        initUI();
                        initData();
                    }
                }
            }
        }
        if (tag == Constants.RequestTag.joinShopCarTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    BaseApplication.getInstance().currentItem=StaticData.INDEX_CAR;
                    BaseApplication.getInstance().carStatus=StaticData.GOOD_CAR_COMPLETE;
                    CommonStatus.carStatus = StaticData.GOOD_CAR_COMPLETE;
                    BaseApplication.getInstance().removeActivity(GoodInfoActivity.this);
                    finish();
                }else{
                    Toast.makeText(GoodInfoActivity.this,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
        if (tag == Constants.RequestTag.selectShopCarZhongLeiTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                countBean = (CountBean) parsedGSON;
                if(countBean.getResult().equals("success")){
                    if(countBean.getData()!=0) {
                        carBadge.setText("" + countBean.getData());
                        carBadge.show();
                    }else{
                        carBadge.hide();
                    }
                }
            }
        }
        if (tag == Constants.RequestTag.dicTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                dictListBean = (DictListBean) parsedGSON;
                if(dictListBean.getResult().equals("success")){
                    Map<String,String> map =new HashMap<String,String>();
                    dicList = dictListBean.getData();
                    for(int i=0;i<dicList.size();i++){
                        map.put(dicList.get(i).getDict_code(),dicList.get(i).getDict_name());
                    }
                    if(goodInfo.getGoods_from()!=null) {
                        if(goodInfo.getGoods_make_time()==null) {
                            info_goodFrom.setText("*本产品由" + map.get(goodInfo.getGoods_from()) + "提供");
                        }else {
                            info_goodFrom.setText("*本产品由" + map.get(goodInfo.getGoods_from()) + "提供");
                        }
                    }
                }
            }
        }
    }


    @Override
    public void errorResonse(String retmeg, int tag) {
        stopProgress();
        Toast.makeText(GoodInfoActivity.this,R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
    /**
     * progressDialog的展示
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new LoadingDialog(GoodInfoActivity.this,"");
            mProgress.setCancelable(true);
            mProgress.show();

        } else {
            if (mProgress.isShowing()) {
                stopProgress();
            } else {
                mProgress.show();
            }
        }
    }


    /**
     * progressDialog的消失
     */
    public void stopProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }

    private void initUI() {

        mViewPagerLay = (LinearLayout) findViewById(R.id.viewpager);
        dot_0 = (ImageView) findViewById(R.id.dot_0);
        dot_1 = (ImageView) findViewById(R.id.dot_1);
        dot_2 = (ImageView) findViewById(R.id.dot_2);
        dot_3 = (ImageView) findViewById(R.id.dot_3);
        dot_4 = (ImageView) findViewById(R.id.dot_4);
        dot_5 = (ImageView) findViewById(R.id.dot_5);
        dot_6 = (ImageView) findViewById(R.id.dot_6);
        dot_7 = (ImageView) findViewById(R.id.dot_7);
        dot_8 = (ImageView) findViewById(R.id.dot_8);
        dot_9 = (ImageView) findViewById(R.id.dot_9);
    }
    private void initData() {
        // 用来显示的点
        dots = new ArrayList<View>();
        // 构造网络图片
        uriList = new ArrayList<String>();
        uriList.clear();
        String lunboImage = goodInfo.getGoods_title_img_url();
        String[] lunboImageArray =  lunboImage.split(",");

        RelativeLayout relative_shopInfo = (RelativeLayout) findViewById(R.id.Relative_shopInfo);

        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        ViewGroup.LayoutParams lp = relative_shopInfo.getLayoutParams();
        lp.width = screenWidth;

        String[] url_params = lunboImageArray[0].replaceAll("\"","").split("&");

        int imageWidth = 0;
        int imageHeight = 0;
        if(url_params.length == 3){
            if(url_params[1].split("=").length ==2 && url_params[2].split("=").length ==2) {
                imageWidth = Integer.parseInt(url_params[1].split("=")[1]);
                imageHeight = Integer.parseInt(url_params[2].split("=")[1]);
            }
        }

        if(imageWidth>0 && imageHeight>0){
            lp.height = new Float(imageHeight*screenWidth/imageWidth).intValue();
        }else{
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }

        relative_shopInfo.setLayoutParams(lp);

        List<String> image = new ArrayList<String>();
        for(int i=0;i<lunboImageArray.length;i++){
            if (!",".equals(lunboImageArray[i])) {
                image.add(lunboImageArray[i]);
            }
        }
        for(int i=0;i<image.size();i++){
            uriList.add(image.get(i));
        }
        if(uriList.size()==4){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
        }

        if(uriList.size()==1){
            dot_0.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
        }

        if(uriList.size()==2){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
        }

        if(uriList.size()==3){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
        }

        if(uriList.size()==5){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
        }
        if(uriList.size()==6){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dot_5.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
            dots.add(findViewById(R.id.dot_5));
        }
        if(uriList.size()==7){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dot_5.setVisibility(View.VISIBLE);
            dot_6.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
            dots.add(findViewById(R.id.dot_5));
            dots.add(findViewById(R.id.dot_6));
        }
        if(uriList.size()==8){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dot_5.setVisibility(View.VISIBLE);
            dot_6.setVisibility(View.VISIBLE);
            dot_7.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
            dots.add(findViewById(R.id.dot_5));
            dots.add(findViewById(R.id.dot_6));
            dots.add(findViewById(R.id.dot_7));
        }
        if(uriList.size()==9){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dot_5.setVisibility(View.VISIBLE);
            dot_6.setVisibility(View.VISIBLE);
            dot_7.setVisibility(View.VISIBLE);
            dot_8.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
            dots.add(findViewById(R.id.dot_5));
            dots.add(findViewById(R.id.dot_6));
            dots.add(findViewById(R.id.dot_7));
            dots.add(findViewById(R.id.dot_8));
        }

        if(uriList.size()==10){
            dot_0.setVisibility(View.VISIBLE);
            dot_1.setVisibility(View.VISIBLE);
            dot_2.setVisibility(View.VISIBLE);
            dot_3.setVisibility(View.VISIBLE);
            dot_4.setVisibility(View.VISIBLE);
            dot_5.setVisibility(View.VISIBLE);
            dot_6.setVisibility(View.VISIBLE);
            dot_7.setVisibility(View.VISIBLE);
            dot_8.setVisibility(View.VISIBLE);
            dot_9.setVisibility(View.VISIBLE);
            dots.add(findViewById(R.id.dot_0));
            dots.add(findViewById(R.id.dot_1));
            dots.add(findViewById(R.id.dot_2));
            dots.add(findViewById(R.id.dot_3));
            dots.add(findViewById(R.id.dot_4));
            dots.add(findViewById(R.id.dot_5));
            dots.add(findViewById(R.id.dot_6));
            dots.add(findViewById(R.id.dot_7));
            dots.add(findViewById(R.id.dot_8));
            dots.add(findViewById(R.id.dot_9));
        }
        mViewPager = new RollViewPagerShopInfo(GoodInfoActivity.this, dots,
                R.drawable.dot_focus, R.drawable.dot_normal,
                new RollViewPagerShopInfo.OnPagerClickCallback() {
                    @Override
                    public void onPagerClick(int position) {

                    }
                });

//        mViewPager.setResImageIds(imageIDs);//设置本地的图片url
        mViewPager.setUriList(uriList);//设置网络图片的url

//        mViewPager.setTitle(title, titles);//不需要显示标题，可以不设置
        mViewPager.startRoll();//不调用的话不滚动
        mViewPagerLay.addView(mViewPager);
    }


    private class InfoHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean isNeedCountTime = false;
                    //①：其实在这块需要精确计算当前时间
                    for (int index = 0; index < goodss.size(); index++) {
                        Goods goods = goodss.get(index);
                        long time = goods.getTime();
                        if (time > 100) {//判断是否还有条目能够倒计时，如果能够倒计时的话，延迟一秒，让它接着倒计时
                            isNeedCountTime = true;
                            goods.setTime(time - 100);
                            days = goods.getTime() / (1000 * 60 * 60 * 24);
                            hours = (goods.getTime()-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
                            minutes = (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
                            seconds = (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000* 60))/(1000);
                            haomiao =  (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000* 60)-seconds*1000)/(100);
                            if(getTimeCha(goodInfo.getStart_time())) {
                                info_time.setText(  days + "天" + hours + ":" + minutes + ":" + seconds+"后开抢");
                            }else {
                                info_time.setText("剩" + days + "天" + hours + ":" + minutes + ":" + seconds+":"+haomiao);
                            }
                        } else {
                            goods.setTime(0);
                            days = goods.getTime() / (1000 * 60 * 60 * 24);
                            hours = (goods.getTime()-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
                            minutes = (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
                            seconds = (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000* 60)) / (1000);
                            haomiao =  (goods.getTime()-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60)-minutes*(1000* 60)-seconds*1000)/(100);
                            if(getTimeCha(goodInfo.getStart_time())) {
                                info_time.setText(  days + "天" + hours + ":" + minutes + ":" + seconds+"后开抢");
                            }else {
                                info_time.setText("剩" + days + "天" + hours + ":" + minutes + ":" + seconds+":"+haomiao);
                            }
                        }
                    }
                    if (isNeedCountTime) {
                        infoHandler.sendEmptyMessageDelayed(1,100);
                    }
                    break;
            }
        }
    }

    //获取当前时间
    private String getNewTime(){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return df.format(new Date());
    }

    /**
     * 得到时间差
     */
    private  Long getTime(String time,String time2){

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        try
        {
            Date d1 = df.parse(time2);
            Date d2 = df.parse(time);
            diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            return diff;
        }
        catch (Exception e)
        {
        }
        return null;
    }

    /**
     * 得到时间差
     */
    private  boolean getTimeCha(String time2){

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date d1 = df.parse(time2);
            Date d2 = df.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            long diff = d1.getTime() - d2.getTime();
            if(diff>0){
                return true;//开抢
            }else{
                return false;
            }

        }
        catch (Exception e)
        {
        }
        return false;
    }




}
